//
//  SYTopBtnView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/8/29.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYTopBtnView.h"

@interface SYTopBtnView ()
@property (strong, nonatomic) NSArray *nameArr;
@property (assign, nonatomic) CGFloat W;
@property (strong, nonatomic) UIButton *selectBtn;//选择的btn
@property (strong, nonatomic) UIView *xhxView;//下划线

@end


@implementation SYTopBtnView

- (instancetype)initWithDataNameArr:(NSArray *)dataNameArr widt:(CGFloat)width {
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        self.nameArr = dataNameArr;
        self.W = width;
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    
    UIView *fgxView = [[UIView alloc] init];
    fgxView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    [self addSubview:fgxView];
    [fgxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    //下划线
    _xhxView = [[UIView alloc] init];
    _xhxView.backgroundColor = [UIColor colorWithHexString:@"#20B097"];
    [self addSubview:_xhxView];
    
    UIButton *contBtn = nil;
    NSMutableArray *titleArr = [NSMutableArray array];
    for (NSInteger i = 0; i < _nameArr.count; i++) {
        NSString *titleStr = _nameArr[i];
        UIButton *title = [UIButton buttonWithType:UIButtonTypeCustom];
        [title setTitle:titleStr forState:UIControlStateNormal];
        title.tag = i;
        title.titleLabel.font = [UIFont systemFontOfSize:14];
        [title addTarget:self action:@selector(titleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:title];
        [titleArr addObject:title];
        if (i == 0) {
            contBtn = title;
            [title setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:UIControlStateNormal];
            
            self.selectBtn = title;
            [title mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.mas_left);
                make.top.equalTo(self.mas_top);
                make.width.equalTo(self).multipliedBy(self.W);
                make.height.mas_equalTo(43);
            }];
            [_xhxView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(title.mas_centerX);
                make.top.equalTo(title.mas_bottom).offset(0);
                make.height.mas_equalTo(2);
                make.width.mas_equalTo(80);
            }];
            
            
        } else {
            [title setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
            
            [title mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(contBtn.mas_right);
                make.top.equalTo(self.mas_top);
                make.width.equalTo(self).multipliedBy(self.W);
                make.height.mas_equalTo(43);
            }];
            contBtn = title;
            
        }
        
    }
    
}

- (void)titleBtnClick:(UIButton *)btn {
    [self.selectBtn setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    self.selectBtn = btn;
    [self.selectBtn setTitleColor:[UIColor colorWithHexString:@"#20B097"] forState:UIControlStateNormal];
    
    [_xhxView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(btn.mas_centerX);
        make.top.equalTo(btn.mas_bottom).offset(0);
        make.height.mas_equalTo(2);
        make.width.mas_equalTo(80);
    }];
    
    if (self.block) {
        self.block(btn.tag);
    }
}


@end
